package com.gree.scmservice.service.impl;

import com.gree.common.entity.TablePageData;
import com.gree.common.utils.DateTime;
import com.gree.common.utils.StringUtils;
import com.gree.scmservice.dao.SysNoticeDao;
import com.gree.scmservice.entity.smcom.Notice;
import com.gree.scmservice.service.GscmSysNoticeService;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Record;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * Created by 180686 on 2021/4/23 9:06
 */

@Service
public class GscmSysNoticeServiceImpl implements GscmSysNoticeService {

    @Autowired
    private SysNoticeDao sysNoticeDao;

    @Autowired
    private Dao dao;

    @Override
    public Notice selectNoticeById(Long noticeId) {
        return null;
    }

    @Override
    public TablePageData<Record> selectNoticeList(Notice notice) {
        TablePageData<Record> recordTablePageData = sysNoticeDao.querySysNoticeList(notice);
        return recordTablePageData;
    }

    @Override
    public int insertNotice(Notice notice) {
        Sql sql1 = Sqls.create("select max(xuno) from smcom01 where styp=@styp and comp in (@comp)");
        sql1.setParam("styp",notice.getStyp()).setParam("comp",notice.getComp());
        sql1.setCallback(Sqls.callback.integer());
        int max = dao.execute(sql1).getInt();

        Sql sql2 = Sqls.create("insert into smcom01(styp,comp,xuno,capt,dsca,idat) values(@styp,@comp,@xuno,@capt,@dsca,to_date(@idat,'yyyy/MM/dd hh24:mi:ss'))");
        sql2.setParam("styp",notice.getStyp())
                .setParam("comp",notice.getComp())
                .setParam("xuno",max+1)
                .setParam("dsca",notice.getDsca())
                .setParam("capt",notice.getCapt())
                .setParam("idat", DateTime.getStringNowTime("yyyy/MM/dd HH:mm:ss"));
        dao.execute(sql2);
        int count = sql2.getUpdateCount();

        List<String> dutalist = notice.getDuta();
        if(dutalist.size()>0){
            StringBuffer sb = new StringBuffer();
            for(String duta:dutalist){
                sb.append(duta);
                sb.append(",");
            }
            String duta = sb.substring(0,sb.length()-1);
            Sql sql3 = Sqls.create("insert into smcom04(styp,comp,xuno,duta) values(@styp,@comp,@xuno,@duta)");
            sql3.setParam("styp",notice.getStyp())
                    .setParam("comp",notice.getComp())
                    .setParam("xuno",max+1)
                    .setParam("duta",duta);
            dao.execute(sql3);
            count+= sql3.getUpdateCount();
        }
        return count;
    }

    @Override
    public int updateNotice(Notice notice) {
        Sql sql = Sqls.create("update smcom01 set capt=@capt,dsca=@dsca,idat=to_date(@idat,'yyyy/MM/dd hh24:mi:ss') where styp=@styp and comp=@comp and xuno=@xuno");
        sql.setParam("styp",notice.getStyp())
                .setParam("comp",notice.getComp())
                .setParam("xuno",notice.getXuno())
                .setParam("dsca",notice.getDsca())
                .setParam("capt",notice.getCapt())
                .setParam("idat", DateTime.getStringNowTime("yyyy/MM/dd HH:mm:ss"));
        dao.execute(sql);
        int count = sql.getUpdateCount();


        Sql sql2 = Sqls.create("select * from smcom04 where styp=@styp and comp=@comp and xuno=@xuno");
        sql2.setParam("styp",notice.getStyp())
                .setParam("comp",notice.getComp())
                .setParam("xuno",notice.getXuno());
        sql2.setCallback(Sqls.callback.entities());
        sql2.setEntity(dao.getEntity(Record.class));
        List<Record> dataList=dao.execute(sql2).getList(Record.class);

        List<String> dutalist = notice.getDuta();
        if(dutalist.size()>0){
            if(dataList.size()==0){
                StringBuffer sb = new StringBuffer();
                for(String duta:dutalist){
                    sb.append(duta);
                    sb.append(",");
                }
                String duta = sb.substring(0,sb.length()-1);
                Sql sql3 = Sqls.create("insert into smcom04(styp,comp,xuno,duta) values(@styp,@comp,@xuno,@duta)");
                sql3.setParam("styp",notice.getStyp())
                        .setParam("comp",notice.getComp())
                        .setParam("xuno",notice.getXuno())
                        .setParam("duta",duta);
                dao.execute(sql3);
                count+= sql3.getUpdateCount();
            }else{
                StringBuffer sb = new StringBuffer(dataList.get(0).getString("duta")).append(",");
                for(String duta:dutalist){
                    sb.append(duta);
                    sb.append(",");
                }
                String duta = sb.substring(0,sb.length()-1);
                Sql sql3 = Sqls.create("update smcom04 set duta = @duta where styp=@styp and comp=@comp and xuno=@xuno");
                sql3.setParam("styp",notice.getStyp())
                        .setParam("comp",notice.getComp())
                        .setParam("xuno",notice.getXuno())
                        .setParam("duta",duta);
                dao.execute(sql3);
                count+= sql3.getUpdateCount();
            }
        }
        return count;
    }



    @Override
    public void deleteNotice(Notice notice) {
        Sql sql1 = Sqls.create("delete from smcom01 where styp=@styp and comp=@comp and xuno=@xuno");
        sql1.setParam("styp",notice.getStyp())
                .setParam("comp",notice.getComp())
                .setParam("xuno",notice.getXuno());
        dao.execute(sql1);
        Sql sql2 = Sqls.create("delete from smcom04 where styp=@styp and comp=@comp and xuno=@xuno");
        sql2.setParam("styp",notice.getStyp())
                .setParam("comp",notice.getComp())
                .setParam("xuno",notice.getXuno());
        dao.execute(sql2);
    }

    @Override
    public void deleteNoticeFile(Notice notice) {
        Sql sql2 = Sqls.create("select * from smcom04 where styp=@styp and comp=@comp and xuno=@xuno");
        sql2.setParam("styp",notice.getStyp())
                .setParam("comp",notice.getComp())
                .setParam("xuno",notice.getXuno());
        sql2.setCallback(Sqls.callback.entities());
        sql2.setEntity(dao.getEntity(Record.class));
        List<Record> dataList=dao.execute(sql2).getList(Record.class);

        List<String> list = Arrays.asList(dataList.get(0).getString("duta").split(","));
        list = new ArrayList<>(list);
        if(list.size()==1){
            Sql sql1 = Sqls.create("delete from smcom04 where styp=@styp and comp=@comp and xuno=@xuno and duta=@duta");
            sql1.setParam("styp",notice.getStyp())
                    .setParam("comp",notice.getComp())
                    .setParam("xuno",notice.getXuno())
                    .setParam("duta",notice.getDuta().get(0));
            dao.execute(sql1);
        }else{
            Iterator<String> it = list.iterator();
            while(it.hasNext())
            {
                if(it.next().equals(notice.getDuta().get(0).toString()))
                {
                    it.remove();
                }
            }

            StringBuffer sb = new StringBuffer();
            for(String duta:list){
                sb.append(duta);
                sb.append(",");
            }
            String duta = sb.substring(0,sb.length()-1);
            Sql sql3 = Sqls.create("update smcom04 set duta = @duta where styp=@styp and comp=@comp and xuno=@xuno");
            sql3.setParam("styp",notice.getStyp())
                    .setParam("comp",notice.getComp())
                    .setParam("xuno",notice.getXuno())
                    .setParam("duta",duta);
            dao.execute(sql3);
        }
    }

    @Override
    public ArrayList uploadNoticeFile(String comp,MultipartFile[] multipartFiles) {
        /*ArrayList dutalist = new ArrayList();
        try {
            List<Record> list = samplePurchaseDao.getNsufePath();
            String upat = list.get(0).getString("upad");
            String usid = list.get(0).getString("usid");
            String pswd = list.get(0).getString("pswd");
            FtpUtil ftp = new FtpUtil(upat, usid, pswd);
            if(multipartFiles != null) {
                for (MultipartFile file : multipartFiles) {
                    InputStream streamIn = null;
                    streamIn = file.getInputStream();
                    String fileName = file.getOriginalFilename();
                    String fpath = comp + "/SMDUT";
                    String duta = DateTime.getDateNowTime().getTime() + fileName;
                    if (ftp.uploadFile(fpath, duta, streamIn)) {
                        dutalist.add(duta);
                    }
                    streamIn.close();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dutalist;*/
        return null;
    }

}
